package com.lkd.handler;


import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompletedMsgHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        log.info("接收到消息");
        TaskCompleteContract taskComplete = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);
        if (taskComplete.getTaskType() == VMSystem.TASK_TYPE_REVOKE) { // 撤机工单
            // 设置为撤机状态
            vendingMachineService.updateStatus(taskComplete.getInnerCode(), VMSystem.VM_STATUS_REVOKE);
        }
        if (taskComplete.getTaskType() == VMSystem.TASK_TYPE_DEPLOY) { // 投放工单
            // 投放之后改为运营状态
            vendingMachineService.updateStatus(taskComplete.getInnerCode(), VMSystem.VM_STATUS_RUNNING);
        }
    }


}
